<?php
/*
	插件概览
*/
global $_RQ,$_AR;
$userid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$setting = $this->plugin_setting('setting');
if($_RQ['op']=='edit'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$item=pdo_get('jy_jd_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$userid));
    $item['logo']=$item['logo']?:$member['avatar'];
	$item['merchname']=$item['merchname']?:$member['nickname'];
	$item['mobile']=$item['mobile']?:$member['mobile'];
	$return['plugin_icon']=$item['logo'];
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array();
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('province'=>'');
	if(!$item['pcd']['province']){
		$item['pcd']=array('province'=>'');
	}
	$item['upass']='';
	$item['myshop_tpl']=$item['myshop_tpl']?:0;
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'商城设置',
		'edit_tab'=>true,
		'edits'=>array(
				array(
					'title'=>'基本信息',
					'list'=>array(
							array('name'=>'merchname','title'=>'商户名称','type'=>'string'),
							array('name'=>'logo','title'=>'商户logo','type'=>'image'),			
							array('name'=>'realname','title'=>'联系人','type'=>'string'),
							array('name'=>'mobile','title'=>'电话','type'=>'string'),
							array('name'=>'uname','title'=>'账户名','type'=>'string'),
							array('name'=>'upass','title'=>'账户密码','type'=>'string'),
							array('name'=>'mid','title'=>'选择管理员','type'=>'choose_title','action'=>'get_member'),					
							array('name'=>'desc','title'=>'商户简介','type'=>'string'),
							array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
							array('name'=>'payrate','title'=>'抽成利率（%）','type'=>'string'),
							array('name'=>'paymid','title'=>'选择收款人','type'=>'choose_title','action'=>'get_member'),			
					)
				),
			)
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	if($citypeisong['isopen']){
		$return['sendtypes']=array(
				'1'=>'同城配送',
				'2'=>'到店自取',
		);
	}else{
		$return['sendtypes']=array(
			'1'=>'快递配送',
			'2'=>'到店自取',
		);
	}
	$return['post']=$return['item']=$item;
	$return['account']=$_SESSION['account'];
	$return['extradata']=array();
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/set');
	}
	$return['province']=cfc('tools')->pcd();
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['merchname']){
		exi('请填写商户名','error');
	}
	if(!$postdata['uname']){
		exi('请填写账户名','error');
	}
	if($postdata){
		$info=pdo_get('jy_jd_user',array('mid'=>$postdata['mid'],'uniacid'=>$_SESSION['uniacid']));	
		$uname=pdo_get('jy_jd_user',array('uname'=>$postdata['uname']));
		$update=array(
			'merchname'=>$postdata['merchname'],
			'logo'=>$postdata['logo'],
			'desc'=>$postdata['desc'],
			'realname'=>$postdata['realname'],
			'mobile'=>$postdata['mobile'],
			'uname'=>$postdata['uname'],
			'payrate'=>$postdata['payrate'],
			'paymid'=>$postdata['paymid'],
			'mid'=>$postdata['mid'],
		);
		if(!$postdata['upass']){
			unset($postdata['upass']);
		}else{
			$update['upass']=base64_encode($postdata['upass']);
		}
		if($postdata['id']){
			if($info && $info['id']!=$postdata['id']){
				exi('一个管理员只能绑定一个账号','error');
			}
			if($uname['id'] && $uname['id']!=$postdata['id']){
				exi('登陆账户名不可以重复','error');	
			}
			pdo_update('jy_jd_user',$update,array('id'=>$postdata['id']));
		}else{
			if($uname['id']){
				exi('登陆账户名不可以重复','error');	
			}
			if($info){
				exi('一个管理员只能绑定一个账号','error');
			}
			$update['uniacid']=$_SESSION['uniacid'];
			$update['mid']=$_SESSION['uid'];
			$update['jointime']=time();
			pdo_insert('jy_jd_user',$update);
		}
	}
	exi('设置成功','','goback');
}
if($_RQ['op']=='diy'){
	$item=pdo_get('jy_jd_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$userid));
    $return['plugin_icon']=$item['logo'];
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array();
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$item['myshop_tpl']=$item['myshop_tpl']?:0;
	$item['setting']=$item['setting'] && $item['setting']!='null'?json_decode($item['setting'],true):array('guide'=>array('title1'=>''));
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'DIY装修小店',
		'edit_tab'=>true,
		'edits'=>array(
				array(
					'title'=>'DIY装修小店',
					'list'=>array(
						array('type'=>'tip','title'=>'【温馨提示】：启用后DIY才生效！<red>有些组件《调用的》DIY时数据会不显示，但是真实预览会显示正常！</red>'),
						array('type'=>'switch','title'=>'启用DIY？','name'=>'opendiy','help'=>'开启后,DIY才生效！'),
						array('type'=>'diypage','title'=>'小店装修','name'=>'setting'),
					)
				),
			)
	);
	$return['htmls']=$htmls;
	//页面设计
	$return['used_navs']=array();
	$demoitems=array(
		array('nid'=>76,'params'=>array()),
		array('nid'=>77,'params'=>array()),
		array('nid'=>78,'params'=>array()),
	);
	$item['items']=$item['items'] && $item['items']!='null'?json_decode($item['items'],true):array();
	if(empty($item['items'][0]['nid'])){
		$item['items']=$demoitems;
	}
	if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
	}
	$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support <>'=>2),array(),'','usenum desc',array('10'));
	if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
	}
	$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support <>'=>2),array(),'','usenum desc',array('10'));
	if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
	}	
	// 应用组件
	$pluginparams = array('type'=>1,'support <>'=>2);
	$plugins = plugin('jy_diypage')->plugins('jy_jd');
	if(!empty($plugins)){
			$pluginparams['plugin_id']=$plugins;
			$navs2=pdo_getall('core_mobile_blocks',$pluginparams,array(),'','usenum desc',array('10'));
			if($navs2){
				$plugintitles=array();
				$plugins=array();
				$i = 0;
				foreach($navs2 as $k=>&$nav){
					$plugintitle=pdo_getcolumn('core_plugins',array('name'=>$nav['plugin_id']),'title');
					$nav['default']=json_decode($nav['default'],true);
				if (!$plugintitle || in_array($plugintitle,$plugintitles)){
						
					}else{
						$plugintitles[$i] = $plugintitle;
						$plugins[$i]['name'] = $nav['plugin_id'];
						$plugins[$i]['plugintitle']=$plugintitle;
						$blocks = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$nav['plugin_id']),array(),'','usenum desc',array('10'));
						foreach($blocks as &$b){
							$b['default']=json_decode($b['default'],true);
						}
						$plugins[$i]['navs'] = $blocks;
						$i  += 1;
					}
					
				}
			}
	}
	$return['navs0']=$navs0;
	$return['navs1']=$navs1;
	$return['navs2']=$plugins;
	$return['edit']=3;
	if(empty($item['setting']['bg'])){
		$item['setting']=array('top'=>array(),'footer'=>array());
	}
	$item['setting']['bg']=!empty($item['setting']['bg'])?:array('bg'=>'');
	$item['setting']['top']=!empty($item['setting']['top'])?:array();	
	$item['setting']['top']['title']=$item['setting']['top']['title']?:$item['title'];
	$return['menus']=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'core','status'=>1),'data');
	$return['menus']=json_decode($return['menus'],true);
	$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$return['menus']['menus'];
	$item['setting']['footer']['chosecolor']=$item['setting']['footer']['chosecolor']?:$_SESSION['customsyscolor'];
	$return['account']=$_SESSION['account'];
	$return['item']=$item;
	$return['extradata']=array();
	$return['cate']=pdo_getall('jy_jd_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['myshop_tpls']=array(
		array('id'=>0,'title'=>'默认风格','image'=>'../addons/jy_jd/resource/images/tpl00.png'),
		array('id'=>1,'title'=>'外卖风格','image'=>'../addons/jy_jd/resource/images/tpl01.png'),
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/shop_index');
	}
	exi($return);
}
if($_RQ['op']=='openvoice'){
	$post=get_postdata();
	$isopenvoice=intval($_RQ['isopenvoice']);
	pdo_update('jy_jd_user',array('isopenvoice'=>$isopenvoice),array('uniacid'=>$_SESSION['uniacid'],'id'=>$userid));
	exi('设置成功！');	
}